package com.mc.mcgoweb.gateway;

import com.alibaba.fastjson.JSON;
import com.mc.common.base.BaseRequest;
import com.mc.common.base.BaseResponse;
import com.mc.service.vo.Order;
import com.mc.service.vo.OrderAddress;
import com.mc.service.vo.OrderFee;
import com.mc.service.vo.OrderVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api")
public class GateWayController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GateWayController.class);


    /**
     * 订单下单进来统一进网关，通过下单传递过来的方法名，然后反射调取相应的方法
     *
     *
     * @param request
     * @return
     */
    @RequestMapping("gateWay")
    public BaseResponse gateWay(@RequestBody BaseRequest request) {

        long begin = System.currentTimeMillis();

        Instant now = Instant.now();


        BaseResponse result = ApiServiceHandler.handle(request.getMethod(), request.getMessageBody());
        Instant now1 = Instant.now();

        LOGGER.info("调用service:{},耗时:{}", request.getMethod(), System.currentTimeMillis() - begin);
        LOGGER.info("耗时:" + Duration.between(now, now1).toMillis() + "ms");
        return result;
    }

    public static void main(String[] args) {
        OrderVo orderVo=new OrderVo();


        Order order=new Order();
        order.setOrderId(10086L);
        order.setOrderNo(10086l);
        order.setOrderStatus(3);
        order.setOrderTime(new Date());

        OrderAddress orderAddress = new OrderAddress();
        orderAddress.setAddressId(10087L);
        orderAddress.setOrderNo(10086l);
        orderAddress.setSender("曼珠沙华");
        orderAddress.setSenderAddress("陕西省西安市");
        orderAddress.setReceiver("付萌朝");
        orderAddress.setReceiverAddress("陕西省延安市");
        orderAddress.setOrderTime(new Date());

        List<OrderFee> feeList=new ArrayList<>();
        OrderFee orderFee1 = new OrderFee();
        orderFee1.setFeeId(10088l);
        orderFee1.setOrderNo(10086l);
        orderFee1.setFeeCode("CT02");
        orderFee1.setIsDelete(0);
        orderFee1.setInFee(new BigDecimal("0"));
        orderFee1.setOutFee(new BigDecimal("0"));

        OrderFee orderFee2 = new OrderFee();
        orderFee2.setFeeId(10088l);
        orderFee2.setOrderNo(10086l);
        orderFee2.setFeeCode("CT02");
        orderFee2.setIsDelete(0);
        orderFee2.setInFee(new BigDecimal("0"));
        orderFee2.setOutFee(new BigDecimal("0"));
        feeList.add(orderFee1);
        feeList.add(orderFee2);
        orderVo.setOrder(order);
        orderVo.setOrderAddress(orderAddress);
        orderVo.setOrderFeeList(feeList);
        System.out.println(JSON.toJSONString(orderVo));

        BaseRequest baseRequest=new BaseRequest();
        baseRequest.setAppKey("111");
        baseRequest.setAppSecret("");
        baseRequest.setMethod("saveOrder");
        baseRequest.setMessageBody(JSON.toJSONString(orderVo));
        baseRequest.setDigest("");
        baseRequest.setVersion("");
        baseRequest.setTimestamp(0L);
        System.out.println(JSON.toJSONString(baseRequest));
    }
}
